﻿using com.ytocargo.Core.BackgroundJobInfos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yto.Common.Dependency;
using Yto.Unitwork;

namespace com.ytocargo.Application.BackgroundJobInfos
{
    public class BackgroundJobInfoServices : IBackgroundJobInfoServices, ITransient
    {
        private readonly IBackgroundJobInfoStore _store;
        public BackgroundJobInfoServices(IBackgroundJobInfoStore store)
        {
            _store = store;
        }

        [Unitwork]
        public async Task CheckTodayJobByJobType(Type type)
        {
            var today = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            var tomorrow = today.AddDays(1);

            var list = await _store.GetListByPredicate(
                            x => x.JobType == type.AssemblyQualifiedName
                            && x.NextTryTime >= today
                            && x.NextTryTime < tomorrow);

        }

        [Unitwork]
        public Task<int> CountTodayJobbyJobType(Type type)
        {
            var today = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            var tomorrow = today.AddDays(1);

            return _store.CountByPredicate(
                            x => x.JobType == type.AssemblyQualifiedName
                            && x.NextTryTime >= today
                            && x.NextTryTime < tomorrow);
        }

        public Task DeleteAsync(BackgroundJobInfo jobInfo)
        {
            return _store.DeleteAsync(jobInfo);
        }

        public Task<List<BackgroundJobInfo>> GetWaitingJobsAsync(int maxCount)
        {
            return _store.GetWaitingJobsAsync(maxCount);
        }

        public Task InsertAsync(BackgroundJobInfo jobInfo)
        {
            return _store.InsertAsync(jobInfo);
        }

        public Task<BackgroundJobInfo> UpdateAsync(BackgroundJobInfo jobInfo)
        {
            return _store.UpdateAsync(jobInfo);
        }
    }
}
